package com.cxf.windex.service.impl;

import com.cxf.windex.base.constant.UserConstant;
import com.cxf.windex.base.util.DateUtil;
import com.cxf.windex.biz.BOrdersBiz;
import com.cxf.windex.biz.BOrdersTaskBiz;
import com.cxf.windex.biz.MoneyBiz;
import com.cxf.windex.entity.BOrders;
import com.cxf.windex.enums.OperateEnum;
import com.cxf.windex.service.ITask;
import com.cxf.windex.task.LogTask;
import com.github.pagehelper.PageHelper;
import java.time.LocalDate;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @program: business_needs
 * @description: 订单任务
 * @author: chenxf
 * @create: 2018-11-09 10:15
 **/
@Slf4j
@Service
public class OrdersTaskImpl implements ITask {
  private String taskName="订单任务";
  @Autowired
  private BOrdersTaskBiz ordersTaskBiz;
  @Autowired
  private BOrdersBiz ordersBiz;
  @Autowired
  private LogTask logTask;

  @Override
  public boolean circlePlan() {
    Example example = new Example(BOrders.class);
    Example.Criteria criteria = example.createCriteria();
    criteria.andLessThan("eTime",  DateUtil.getByLocalDate(LocalDate.now()) );
    criteria.andEqualTo("status","1");//进行中的进行设置
    PageHelper.startPage(1, 1);

    example.setOrderByClause("crt_time asc");
    List<BOrders> items = ordersBiz.selectByExample(example);
    if(items.size()==0){
      return false;
    }
    BOrders orders=items.get(0);
    //平台广告无需退回
    if(StringUtils.isNotBlank(orders.getShopWxopenid())){

      //退回未支付余额
      logTask.payMoney(orders.getShopWxopenid(),orders.getTotalPrice().subtract(orders.getPayPrice()),OperateEnum.COST_BACK,UserConstant.SHOP_RANGE,orders.getId(),true);

    }

    //设置订单已经过期
    ordersBiz.updateSelectiveById(BOrders.builder().id(orders.getId()).status("2").build());
    return true;
  }

  @Override
  public void singlePlan() {
    ordersTaskBiz.unDone();
  }

  @Override
  public String getName() {
    return taskName;
  }
}
